package com.tgy.core.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.BlogFileMapper;
import com.tgy.core.service.BlogFileService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.BlogFile;
import com.tgy.result.LayuiTableResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 资源文件信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class BlogFileServiceImpl extends ServiceImpl<BlogFileMapper, BlogFile> implements BlogFileService {

    @Resource
    private BlogFileMapper baseMapper;

    /**
     * 查询资源文件信息
     *
     * @param id 资源文件信息主键
     * @return 资源文件信息
     */
    @Override
    public BlogFile queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询资源文件信息列表
     *
     * @param bo 资源文件信息
     * @return 资源文件信息
     */
    @Override
    public TableDataInfo<BlogFile> queryPageList(BlogFile bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogFile> lqw = buildQueryWrapper(bo);
        Page<BlogFile> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(BlogFile bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogFile> lqw = buildQueryWrapper(bo);
        Page<BlogFile> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询资源文件信息列表
     *
     * @param bo 资源文件信息
     * @return 资源文件信息
     */
    @Override
    public List<BlogFile> queryList(BlogFile bo) {
        LambdaQueryWrapper<BlogFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<BlogFile> buildQueryWrapper(BlogFile bo) {
        LambdaQueryWrapper<BlogFile> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getTitle()),BlogFile::getTitle,bo.getTitle());
        lqw.like(StrUtil.isNotBlank(bo.getRemark()),BlogFile::getRemark,bo.getRemark());
        lqw.orderByDesc(BlogFile::getCreatedTime);
        return lqw;
    }

    /**
     * 新增资源文件信息
     *
     * @param bo 资源文件信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(BlogFile bo) {
        // validEntityBeforeSave(BlogFile);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改资源文件信息
     *
     * @param bo 资源文件信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(BlogFile bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(BlogFile);
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除资源文件信息
     *
     * @param ids 需要删除的资源文件信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        /*if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }*/
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
